import { Request, Response, NextFunction } from 'express';
import _ from 'lodash';
import { getTokenCookie, updateTokenCookie } from './tokenCookie.service';

/**
 * 获取内容
 */
export const index = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  try {
    const data = await getTokenCookie();

    response.send(data);
  } catch (error) {
    console.log("err: ", error)
  }
}

/**
 * 更新内容
 */
export const update = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取更新内容 ID
  const { postId } = request.params;

  // 获取内容
  const post = _.pick(request.body, [
    'token',
    'cookie',
    'time',
    'end_time',
  ]);

  // 更新
  try {
    const data = await updateTokenCookie(post, parseInt(postId, 10));
    response.send(data);
  } catch (error) {
    next(error);
  }
}
